<?php

namespace app\controller\wmapi\partner;

use App\Library\Y;
use app\model\WaAdmins;
use app\model\WaSupplierTransferExportFee;
use app\model\WaSupplierTransferExportFeeRelevance;
use app\model\WpProductsCans;
use app\controller\wmapi\IndexController;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use plugin\admin\app\common\Util;
use plugin\admin\app\controller\Crud;
use plugin\admin\app\model\User;
use support\Request;
use support\Response;
use Tinywan\Validate\Validate;
use Workerman\Worker;


class WaSupplierTransferExportFeeController  extends Validate
{

    /**
     * 转移待出口列表
     * @param Request $request
     * @return Response
     */
    public function transferExportFeeList(Request $request){
        $param = $request->all();
        $page = page($param['page']);
        $pageNum = pageNum($param['pageNum']);
        $fields = ['*'];
        $condition = [];
        if ($param['phone']) {
            $condition [] = ['phone', 'like', '%' . $param['phone'] . '%'];
        }

        $condition [] = ['status', '!=', 4];
        $res = WaSupplierTransferExportFee::list($page, $pageNum, $fields, $condition);
        return IndexController::successed($res, '转移待出口费用列表');
    }


    /**
     * 转移待出口列表 - 不分页
     * @param Request $request
     * @return Response
     */
    public function transferExportFeeAll(Request $request){
        $param = $request->all();
        $fields = ['*'];
        $condition [] = ['status', '!=', 4];
        $res = WaSupplierTransferExportFee::all( $fields, $condition);
        return IndexController::successed($res, '转移待出口费用列表不分页');
    }


    /**
     * 转移待出口创建
     * @param Request $request
     * @return \support\Response
     */
    public function transferExportFeeCreate(Request $request)
    {

//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));

        /**
         * 验证规则
         */
        $data = [
            'name' => $param['name'], // 上牌供应商名字
            'credit_code' => $param['credit_code'],// 组织机构信用代码
            'linkman' => $param['linkman'], // 联系人
            'phone' => $param['phone'],
            'email' => $param['email'],
            'legal_person_name' => $param['legal_person_name'], // 法人姓名
            'legal_person_phone' => $param['legal_person_phone'], // 法人手机
            'legal_person_identity_card' => $param['legal_person_identity_card'], // 法人身份证号码
            'address' => $param['address'],
            'remark' => $param['remark'], // 备注
            'registered_address' => $param['registered_address'], // 注册地址
            'business_scope' => $param['business_scope'], // 经营范围
            'created_at' => dateTime(),
            'admin_id' => $adminId->id, // 创建人
            'status' => 0,

            'legal_person_identity_left' => $param['legal_person_identity_left'], // 法人身份证正面
            'door_imgs' => json_encode($param['door_imgs']), // 门店照片 多张 数组格式
            'legal_person_identity_right' => $param['legal_person_identity_right'], // 法人身份证反面
            'business_license_img' => $param['business_license_img'], // 营业执照号照片

            'province' => $param['province'],
            'city' => $param['city'],
//            'subbranch_name' => $param['subbranch_name'],

        ];
        if ($param['account_information']){
            $data['account_information'] =   json_encode($param['account_information']); // 账户信息
        }

//        $validate = self::validateAdd($data);
//        if (!$validate) {
//            return IndexController::errored('', '国际货运代理入参有误');
//        }
        $where = [];
//        $where [] = ['phone' ,'=', $param['phone']];
//        $where [] = ['status' ,'!=', 4];
        $whereOr = [];
        $whereOr [] = ['name' ,'=', $param['name']];
        $whereOr [] = ['status' ,'!=', 4];
        $find = WaSupplierTransferExportFee::findOr($where,$whereOr);
        $find = json_decode($find);
        if ($find) {
            if (property_exists($find, 'id')) {
                return IndexController::errored('', '国际货运代理创建失败,国际货运代理名称、类型、地区已存在');
            }
        }
        \support\Db::beginTransaction();
        try {
            $res = WaSupplierTransferExportFee::created($data);
            if (!$res) {
                return IndexController::errored($res, '国际货运代理创建失败');
            }
            \support\Db::commit();
            echo $res;
            return IndexController::successed($res, '国际货运代理创建成功');
        } catch (\Throwable $exception) {
            IndexController::logError('transferExportFeeCreate', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '国际货运代理创建失败');
        }

    }

    public static function validateAdd($data)
    {
        $rule = [
            'name' => 'require', // 上牌供应商名字
            'address' => '',
            'phone' => 'require',
            'email' => 'require',
            'credit_code' => 'require',// 组织机构信用代码
            'linkman' => 'require', // 联系人
            'legal_person_name' => 'require', // 法人姓名
            'legal_person_phone' => 'require', // 法人手机
            'legal_person_identity_card' => 'require', // 法人身份证号码
            'registered_address' => '', // 注册地址
            'business_license_img' => '', // 营业执照号照片
//            'billing_type' => 'require', // 1 增值税普通发票 2 增值税专用发票
            'payment_account_name' => 'require', // 开户名
            'payment_account' => 'require', // 银行账号
            'bank_name' => 'require', // 开户银行
//            'subbranch_name' => 'require', // 开户支行
            'business_scope' => '', // 经营范围
        ];


        $validate = \Tinywan\Validate\Facade\Validate::rule($rule);

        if (!$validate->check($data)) {
            return $validate->getError();
        }
        return true;
    }

    /**
     * 转移待出口更新
     * @param Request $request
     * @return \support\Response
     */
    public function transferExportFeeEdit(Request $request)
    {
//        process($request);
        $param = $request->all();
        $adminId = json_decode(adminId($request));
        /**
         * 验证规则
         */
        $data = [
            'name' => $param['name'], // 上牌供应商名字
            'credit_code' => $param['credit_code'],// 组织机构信用代码
            'linkman' => $param['linkman'], // 联系人
            'phone' => $param['phone'],
            'email' => $param['email'],
            'legal_person_name' => $param['legal_person_name'], // 法人姓名
            'legal_person_phone' => $param['legal_person_phone'], // 法人手机
            'legal_person_identity_card' => $param['legal_person_identity_card'], // 法人身份证号码
            'address' => $param['address'],
            'remark' => $param['remark'], // 备注
            'registered_address' => $param['registered_address'], // 注册地址
            'business_scope' => $param['business_scope'], // 经营范围

            'created_at' => dateTime(),
            'admin_id' => $adminId->id, // 创建人
            'status' => 0,

            'legal_person_identity_left' => $param['legal_person_identity_left'], // 法人身份证正面
            'door_imgs' => json_encode($param['door_imgs']), // 门店照片 多张 数组格式
            'legal_person_identity_right' => $param['legal_person_identity_right'], // 法人身份证反面
            'business_license_img' => $param['business_license_img'], // 营业执照号照片

            'province' => $param['province'],
            'city' => $param['city'],
//            'subbranch_name' => $param['subbranch_name'],

        ];
        if ($param['account_information']){
            $data['account_information'] =   json_encode($param['account_information']); // 账户信息
        }
//        $data['international_logistics_insurance_fee'] = $param['international_logistics_insurance_fee']; // 国际物流保险费

//        $validate = self::validateAdd($data);
//        if (!$validate) {
//            return IndexController::errored('', '国际货运代理入参有误');
//        }

        $where = [];
//        $where [] = ['phone' ,'=', $param['phone']];
//        $where [] = ['status' ,'!=', 4];
        $whereOr = [];
        $whereOr [] = ['name' ,'=', $param['name']];
        $whereOr [] = ['status' ,'!=', 4];
        $find = WaSupplierTransferExportFee::findOr($where,$whereOr);
        $find = json_decode($find);
        if ($find){
            if (property_exists($find, 'id')) {
                if ($param['transfer_export_id'] != $find->id) {
                    return IndexController::errored('', '国际货运代理更新失败,国际货运代理电话已存在');
                }
            }
        }

        $condition = [];
        $condition [] = ['id', '=', intval($param['transfer_export_id'])];
        \support\Db::beginTransaction();
        try {
            $res = WaSupplierTransferExportFee::updated($condition, $data);

            \support\Db::commit();
            return IndexController::successed($res, '国际货运代理更新成功');
        } catch (\Throwable $exception) {
            IndexController::logError('transferExportFeeEdit', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '国际货运代理更新失败');
        }
    }

    /**
     * 转移待出口删除
     * @param Request $request
     * @return Response
     */
    public function transferExportFeeDelete(Request $request): Response
    {
//        process($request);
        $adminId = json_decode(adminId($request));
        $param = $request->all();
        $data = [
            'status' => 4, // 4 删除
            'admin_id' => $adminId->id,
            'updated_at' => date('y-m-d h:m:s', time()),
        ];
        $where = [
            'id' => $param['transfer_export_id'],
        ];

        \support\Db::beginTransaction();
        try {
            $res = WaSupplierTransferExportFee::updated($where, $data);
            if (!$res){
                return IndexController::errored('', '转移待出口-删除失败');
            }
//            $del = WaSupplierTransferExportFee::deleted(['id' => $param['transfer_export_id']]);
            $relevance_del = WaSupplierTransferExportFeeRelevance::delete(['parent_id' => $param['transfer_export_id']]);
            \support\Db::commit();
            return IndexController::successed('', '转移待出口已删除');
        } catch (\Throwable $exception) {
            IndexController::logError('transferExportFeeDelete', $param, $adminId, $exception);
            \support\Db::rollBack();
            return IndexController::errored($exception, '转移待出口删除失败');
        }
    }


    /**
     * 转移待出口详情
     * @param Request $request
     * @return Response
     */
    public function transferExportFeeDetail(Request $request): Response
    {
//        process($request);
        $adminId = json_decode(adminId($request));
        $param = $request->all();
        $where = [
            'id' => $param['transfer_export_id'],
        ];
        $detail = WaSupplierTransferExportFee::find($where);
        return IndexController::successed($detail, '转移待出口详情');
    }


}